package br.com.werp.suporte.Maquinas;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import com.egen.util.jdbc.*;
import com.egen.util.text.*;

/**
 * Creation Date 29/09/2004 11:21:48
 * Last Modify Date 28/12/2006 10:39:28
 */

public class MaquinasDetalheAction extends com.egen.util.struts.AbstractAction {

  public ActionForward perform_salvar_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {


      //####################################################################################
      MaquinasDetalheActionForm f = (MaquinasDetalheActionForm) form;
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();
      String usuario = (String) session.getAttribute("j_username");

      suporte.manterTipoMemoria(form, usuario);
      suporte.atualizarPlacaVideo((List) session.getAttribute("placas"),
          f.getMaqn_id(), usuario);
      suporte.atualizarAplicativos(
        (List) session.getAttribute("aplicativos"), f.getMaqn_id(),
        usuario);
      suporte.atualizarHd((List) session.getAttribute("hds"),
          f.getMaqn_id(), usuario);
      suporte.atualizarPlacaRede((List) session.getAttribute("rede"),
          f.getMaqn_id(), usuario);
      suporte.atualizarSo((List) session.getAttribute("so"),
          f.getMaqn_id(), usuario, form);

      suporte.atualizarUsuarioMaquina(form,
          (List) session.getAttribute("usuarios"), usuario);

      suporte.atualizarManutencaoMaquina(form,
          (List) session.getAttribute("manutencoes"), usuario);


      suporte.atualizarMaquina(form, usuario);

      session.setAttribute("placas",
          suporte.obterPlacasVideo(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));
      session.setAttribute("hds",
          suporte.obterHds(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));
      session.setAttribute("aplicativos",
          suporte.obterAplicativos(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));
      session.setAttribute("rede",
          suporte.obterPlacasRede(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));

      session.setAttribute("so",
          suporte.obterSo(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));

      session.setAttribute("usuarios",
          suporte.pesquisarUsuarioMaquina(f.getMaqn_id()));
      session.setAttribute("manutencoes", suporte.pesquisarManutencao(form));


      //####################################################################################

      actionForward = mapping.findForward("pesquisar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_excluir_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      // j = new com.egen.util.jdbc.JdbcUtil();
      //------------------------------------------------------------------------------
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();
      String usuario = (String) session.getAttribute("j_username");
      suporte.excluirMaquina(form, usuario);
      //-------------------------------------------------------------------------------


      actionForward = mapping.findForward("pesquisar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_back_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();


      actionForward = mapping.findForward("voltar");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_select_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      //###############################################################################
      MaquinasDetalheActionForm f = (MaquinasDetalheActionForm) form;
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();
      f.setMaqn_memoria_tipo(suporte.obterTipoMemoria((f.getMaqn_id())));

      session.setAttribute("placas",
          suporte.obterPlacasVideo(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));

      session.setAttribute("hds",
          suporte.obterHds(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));

      session.setAttribute("aplicativos",
          suporte.obterAplicativos(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));

      session.setAttribute("rede",
          suporte.obterPlacasRede(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));

      session.setAttribute("so",
          suporte.obterSo(
            java.lang.Integer.valueOf(f.getMaqn_id()).intValue()));


      session.setAttribute("usuarios",
          suporte.pesquisarUsuarioMaquina(f.getMaqn_id()));

      session.setAttribute("manutencoes", suporte.pesquisarManutencao(form));

      suporte.carregarMaquina(form);

      //###############################################################################

      j = new com.egen.util.jdbc.JdbcUtil();
      // select_action(j, mapping, form, request, response);
      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }
  private void select_action(JdbcUtil j, ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession(true);
    MaquinasDetalheActionForm f = (MaquinasDetalheActionForm) form;
    br.com.werp.suporte.dbobj.table.Maquina table =
      new br.com.werp.suporte.dbobj.table.Maquina();
    String[][] select = null;
    String[] order = null;
    String[] groupby = null;
    String[] having = null;
    String[] array_placaVideo = f.getPlacaVideo();
    String in_placaVideo = "";

    Object[][] where = { {"Maqn_id","=",
        com.egen.util.text.FormatNumber.toInt(f.getMaqn_id())}, {"Maqn_nome",
            "like", f.getMaqn_nome()}, {"Maqn_processador","=",
                com.egen.util.text.FormatNumber.toInt(f.getMaqn_processador())}, {"Maqn_placamae", "=",
                    com.egen.util.text.FormatNumber.toInt(f.getMaqn_placamae())}, {"Maqn_clock",
                        "=", com.egen.util.text.FormatNumber.toInt(f.getMaqn_clock())}, {"Maqn_memoria", "=",
                            com.egen.util.text.FormatNumber.toInt(f.getMaqn_memoria())}, };
    Vector res_Maquina =
      j.select(table, select, where, groupby, having, order);
    session.setAttribute("res_Maquina",res_Maquina);
    if (res_Maquina != null && res_Maquina.size() > 0) {
      br.com.werp.suporte.dbobj.table.Maquina r =
        (br.com.werp.suporte.dbobj.table.Maquina)
        res_Maquina.elementAt(0);
      f.setMaqn_id(r.getMaqn_id() + "");
      f.setMaqn_nome((r.getMaqn_nome() != null) ? r.getMaqn_nome() + "":"");
      f.setMaqn_processador(r.getMaqn_processador() + "");
      f.setMaqn_placamae(r.getMaqn_placamae() + "");
      f.setMaqn_clock(r.getMaqn_clock() + "");
      f.setMaqn_memoria(r.getMaqn_memoria() + "");
    } else {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("warn.norowsselected",""));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
    }
    session.setAttribute(mapping.getName(), form);
  }

  public ActionForward perform_placaRede_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      session.setAttribute(mapping.getName(), form);
      actionForward = mapping.findForward("same");
      //###############################################################################
      MaquinasDetalheActionForm f = (MaquinasDetalheActionForm) form;
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();

      List list = (List) session.getAttribute("rede");
      suporte.manterPlacaRede(f.getPlacaRedeDisp(), f.getPpRede(),
          f.getSwRede(), f.getIpRede(), list, f.getPlacaRedeOP(),
          f.getPlacaRede(), f.getOnboardRede());
      //###############################################################################
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

  public ActionForward perform_aplicativos_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    try {
      session.setAttribute(mapping.getName(), form);
      actionForward = mapping.findForward("same");
      //###############################################################################
      MaquinasDetalheActionForm f = (MaquinasDetalheActionForm) form;
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();

      List list = (List) session.getAttribute("aplicativos");
      suporte.manterAplicativos(f.getAplicativosDisp(), list,
          f.getAplicativosOP(), f.getAplicativos());
      //###############################################################################

    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    return actionForward;
  }

  public ActionForward perform_hds_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();


      actionForward = mapping.findForward("same");
      //###############################################################################
      MaquinasDetalheActionForm f = (MaquinasDetalheActionForm) form;
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();

      List list = (List) session.getAttribute("hds");
      suporte.manterHds(f.getHdDisp(), list, f.getHdsOP(), f.getHd());
      //###############################################################################

    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_placaVideo_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();


      actionForward = mapping.findForward("same");
      //###############################################################################
      MaquinasDetalheActionForm f = (MaquinasDetalheActionForm) form;
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();

      List list = (List) session.getAttribute("placas");
      suporte.manterPlacaVideo(f.getPlacaVideoDisp(), list,
          f.getPlacaVideoOP(), f.getPlacaVideo(), f.getOnboard());
      //###############################################################################

    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_sO_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      j = new com.egen.util.jdbc.JdbcUtil();


      actionForward = mapping.findForward("same");
      //###############################################################################
      MaquinasDetalheActionForm f = (MaquinasDetalheActionForm) form;
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();

      List list = (List) session.getAttribute("so");
      suporte.manterSo(f.getSoDisp(), list, f.getSoOp(), f.getSo());
      //     suporte.manterSo(f.getsoDisp(),list, f.getsoOP(), f.getso());
      //###############################################################################

    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_manter_usuario_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      // j = new com.egen.util.jdbc.JdbcUtil();
      //----------------------------------------------------------------------
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();
      List usuarios = (List) session.getAttribute("usuarios");
      suporte.manterUsuarioMaquina(form, usuarios);
      //----------------------------------------------------------------------

      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

  public ActionForward perform_manutencao_action(ActionMapping mapping,
      ActionForm form, HttpServletRequest request,
      HttpServletResponse response) {
    HttpSession session = request.getSession(true);
    ActionForward actionForward = null;
    com.egen.util.jdbc.JdbcUtil j = null;
    try {
      //j = new com.egen.util.jdbc.JdbcUtil();

      //--------------------------------------------------------------------------
      br.com.werp.regras.geral.Suporte suporte =
        br.com.werp.regras.geral.FabricaSubSistema.getSuporte();
      List manutencoes = (List) session.getAttribute("manutencoes");
      suporte.manterManutencaoMaquina(form, manutencoes);

      //--------------------------------------------------------------------------

      actionForward = mapping.findForward("same");
    } catch (Exception e) {
      ActionErrors errors = new ActionErrors();
      errors.add("ActionErrors.GLOBAL_ERROR",
          new ActionError("error.action.exception",
              com.egen.util.system.Error.getMessage(e)));
      request.setAttribute(org.apache.struts.Globals.ERROR_KEY, errors);
      session.setAttribute("exception",
          com.egen.util.system.Error.getDescription(e));
      actionForward = mapping.findForward("same");
    }
    finally { if (j != null) {
          j.close();
        }
            } return actionForward;
  }

}
